/*
 * Copyright (c) 2024 Black Sesame Technologies
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "dal/allocator.h"
#include "dal_fastdds/listener_impl/DataWriterListenerImpl.hpp"
#include "dal_fastdds/entity_impl/DalDataWriterImpl.hpp"
#include "dal_fastdds/utils/log.hpp"
#include "dal_fastdds/utils/EntityMappingManager.hpp"

DataWriterListenerImpl::DataWriterListenerImpl(dal_datawriter_listener_callbacks_t callbacks)
    : callbacks_(callbacks)
{
}

DataWriterListenerImpl::~DataWriterListenerImpl() {}

dal_datawriter_listener_callbacks_t DataWriterListenerImpl::GetCallbacks()
{
    return callbacks_;
}

void DataWriterListenerImpl::on_liveliness_lost(DataWriter* writer,
                                                const LivelinessLostStatus& status)
{
    if (callbacks_.on_liveliness_lost == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_liveliness_lost_status_t dal_status;
    StatusConverter::GetInstance()->ConvertLivelinessLostStatusFromFastddsToDal(&status,
                                                                                &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_liveliness_lost)(dal_writer, &dal_status, data);
}

void DataWriterListenerImpl::on_offered_deadline_missed(DataWriter* writer,
                                                        const OfferedDeadlineMissedStatus& status)
{
    if (callbacks_.on_offered_deadline_missed == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_offered_deadline_missed_status_t dal_status;
    StatusConverter::GetInstance()->ConvertOfferedDeadlineMissedStatusFromFastddsToDal(&status,
                                                                                       &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_offered_deadline_missed)(dal_writer, &dal_status, data);  
}

void DataWriterListenerImpl::on_offered_incompatible_qos(DataWriter* writer,
                                                         const OfferedIncompatibleQosStatus& status)
{
    if (callbacks_.on_offered_incompatible_qos == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        return;
    }

    dal_offered_incompatible_qos_status_t dal_status;
    StatusConverter::GetInstance()->ConvertOfferedIncompatibleQosStatusFromFastddsToDal(
        &status, &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_offered_incompatible_qos)(dal_writer, &dal_status, data);
}

void DataWriterListenerImpl::on_publication_matched(DataWriter* writer,
                                                    const PublicationMatchedStatus& info)
{
    if (callbacks_.on_publication_matched == nullptr)
        return;

    dal_datawriter_t* dal_writer = EntityMappingGetDataWriter(writer);
    if (!dal_writer) {
        dal_datawriter_t* dal_writer_tmp = dal_datawriter_allocate();
        dal_writer = EntityMappingGetOrAddDataWriter(writer, dal_writer_tmp);
        if (dal_writer_tmp != dal_writer) {
            dal_datawriter_free(dal_writer_tmp);
        }
    }

    dal_publication_matched_status_t dal_status;
    StatusConverter::GetInstance()->ConvertPublicationMatchedStatusFromFastddsToDal(&info,
                                                                                    &dal_status);

    void* data = callbacks_.data_;
    (callbacks_.on_publication_matched)(dal_writer, &dal_status, data);
}
